package com.gusha.service;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gusha.dao.SysRepositoryDao;
import com.gusha.model.SysRepository;
import com.gusha.model.User;


@Service
@Transactional
public class SysRepositoryService {
	@Autowired
	private SysRepositoryDao sysRepositoryDao;
	public Page<SysRepository> findAll(Pageable pageable, SysRepository sysRepository) {
	    @SuppressWarnings("serial")
	    Page<SysRepository> result = sysRepositoryDao.findAll(new Specification<SysRepository>() {
	        @Override
	        public Predicate toPredicate(Root<SysRepository> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
	            List<Predicate> list = new ArrayList<Predicate>();
	            if (sysRepository.getId() != null) {
	                list.add(cb.equal(root.get("id").as(BigInteger.class),sysRepository.getId()));
	            }
	            if (sysRepository.getProductType()!= null) {
	                list.add(cb.equal(root.get("productType").as(BigInteger.class),sysRepository.getProductType()));
	            }
	            if (sysRepository.getProductName()!= null) {
	                list.add(cb.equal(root.get("productName").as(String.class),sysRepository.getProductName()));
	            }
	            if (sysRepository.getProductNum()!= null) {
	                list.add(cb.equal(root.get("productNum").as(BigInteger.class),sysRepository.getProductNum()));
	            }
	            if (sysRepository.getUser()!= null) {
	                list.add(cb.equal(root.get("user").as(User.class),sysRepository.getUser()));
	            }
	            if (sysRepository.getProductImg()!= null) {
	                list.add(cb.equal(root.get("productImg").as(String.class),sysRepository.getProductImg()));
	            }
//	            if (sysRepository.getSysWarehouse()!= null) {
//	                list.add(cb.equal(root.get("sysWarehouse").as(String.class),sysRepository.getSysWarehouse()));
//	            }
	            
	            if (sysRepository.getIsDelete()!= null) {
	                list.add(cb.equal(root.get("isDelete").as(BigInteger.class),sysRepository.getIsDelete()));
	            }
	            if (sysRepository.getIsCheck()!= null) {
	                list.add(cb.equal(root.get("isCheck").as(BigInteger.class),sysRepository.getIsCheck()));
	            }
	            if (sysRepository.getAddTime() != null) {
                    list.add(cb.equal(root.get("addTime").as(Date.class),sysRepository.getAddTime()));
                }
	            Predicate[] p = new Predicate[list.size()];
	            return cb.and(list.toArray(p));
	        }
	    },pageable);
	     return result;
	}
}
